# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

from coin.exchange.base.kr_rest.product import NativeProduct, NormProduct
from coin.exchange.quoinex_v2.kr_rest.constants import (
    symbol_list,
    quote_currency_list,
    base_currency_list,
    product_id_dict,
    inv_product_id_dict,
)
from coin.exchange.quoinex_v2.kr_rest.currency import QuoinexCurrency


def get_active_symbol_list():
  res = []
  quote = 'JPY'
  for base in base_currency_list:
    if base != quote:
      product = QuoinexProduct.FromStrPair(base, quote)
      if product.native_symbol in symbol_list:
        res.append(product.native_symbol)
  return res


class QuoinexProduct(NativeProduct):
  _exchange = 'Quoinex'
  NativeCurrencyType = QuoinexCurrency

  def __init__(self, base, quote):
    assert base.native_currency in base_currency_list, base
    assert quote.native_currency in quote_currency_list, quote
    assert base != quote, (base, quote)
    native_symbol = '%s%s' % (base.native_currency, quote.native_currency)
    super().__init__(base, quote, native_symbol)

  @property
  def product_id(self):
    return product_id_dict[self.native_symbol]

  @classmethod
  def FromStrNativeProductImpl(cls, product, current_datetime):
    assert isinstance(product, str), type(product)
    assert product in symbol_list, product
    for quote in quote_currency_list:
      if product.endswith(quote):
        base = product[:product.index(quote)]
        assert base in base_currency_list
        return QuoinexProduct(QuoinexCurrency.FromStrNativeCurrency(base),
                              QuoinexCurrency.FromStrNativeCurrency(quote))
    raise ValueError(product)

  @classmethod
  def FromProductId(cls, product_id):
    native_symbol = inv_product_id_dict[product_id]
    return cls.FromStrNativeProduct(native_symbol)
